/**
 * 
 */
/*共用功能初始化*/
$(function(){

		
});

/*辅助类 供以后深层次抽象化功能使用*/
/*系统工具类*/
window.util={};
/*模型类*/
window.model={};
/*是否调试*/
window.util.debug=true;
/*js信息*/

util.echo=function(str){
	if(util.debug){
		if($.browser.msie){
			alert("请在Firefox或Chrome控制台下查看信息");
		}else{
			console.log(str);
		}	
	}
};
/*系统提示工具*/
util.message=function(type,str){
	  $.messager.show({
          title: type,
          msg: str,
          style:{
              right:'',
              bottom:''
          }
      });
};



/*显示添加弹出框*/
function showAdd(dlgid,dlgtitle,formid,addurl){
	$(dlgid).dialog('open').dialog('setTitle',dlgtitle);
    $(formid).form('clear');
    url = addurl; 
}
/*显示编辑弹出框*/
function showEdit(gridid,dlgid,dlgtitle,formid,editurl){
	var checkedItem = $(gridid).datagrid('getChecked');
    if(checkedItem.length!=1){
    	util.message("系统提示", "请您正确选择数据!");
    	return;
    }
	  /*单选*/
	  var row = $(gridid).datagrid('getSelected');
	  $(dlgid).dialog('open').dialog('setTitle',dlgtitle);
      $(formid).form('load',row);
      url = editurl+''+row.id;
      
}
/*保存新实体实例*/
function save(dlgid,gridid,formid,url){
	 $(formid).form('submit',{
         url: 'data/index.json',
         onSubmit: function(){
        	 util.echo(url);
             return $(this).form('validate');
         },
         success: function(result){
             var result = {errorMsg:false};
             if (result.errorMsg){
            	 util.message("错误信息", result.errorMsg);
             } else {
            	 
            	 util.echo(url+"----");

                 $(dlgid).dialog('close');        // close the dialog
                 $(gridid).datagrid('reload');    // reload the user data
             }
         }
     });
	 
	
}
/**/
function  del(gridid,delurl){
	/*单选*/
	/* var row = $(gridid).datagrid('getSelected');
	 console.log(row);
     if (row){
         $.messager.confirm('操作确认','您真的确定要删除该条数据?',function(r){
             if (r){
            	
                 $.post(delurl,{id:row.id},function(result){
                	 result={success:false};
                	
                     if (result.success){
                         $(gridid).datagrid('reload');    // reload the user data
                     } else {
                         util.message("错误信息","删除数据出错请检查");
                     }
                 },'json');
             }
         });
     }else{
    	 util.message("系统提示", "请您选择需要删除的数据!");
     }*/
     
	
    
     /*多选*/
     var checkedItems = $(gridid).datagrid('getChecked');
     if(checkedItems.length==0){
     	util.message("系统提示", "请您正确选择数据!");
     	return;
     }
	 var names = [];
	 $.each(checkedItems, function(index, item){
	 names.push(item.id);
	 });               
	
     util.echo("删除操作");
}

function view(gridid,dlgid,dlgtitle){
	var checkedItem = $(gridid).datagrid('getChecked');
    if(checkedItem.length!=1){
    	util.message("系统提示", "请您正确选择数据!");
    	return;
    }
	  /*单选*/
	  var row = $(gridid).datagrid('getSelected');
	  $(dlgid).dialog('open').dialog('setTitle',dlgtitle);

}

function submitForm(formid,gridid){
	/*$(formid).form('submit', {  
	    url:'data/index.json',  
	    onSubmit: function(){  
	    	var isValid = $(this).form('validate');
			if (!isValid){
				$.messager.progress('close');	// hide progress bar while the form is invalid
			}
			return isValid;	// return false will stop the form submission
	    },  
	    success:function(data){  
	    	$.messager.progress('close');	
	    }  
	});  */
	
	var isValid = $(formid).form('validate');
	if (!isValid){
		$.messager.progress('close');	// hide progress bar while the form is invalid
		return;
	}else{
		util.echo($(formid).serializeArray());
		$(gridid).datagrid('load', requestParamToJson($(formid).serializeArray()));  
	}
	

	
}
function clearForm(formid){
    $(formid).form('clear');
}

window.util.closeDialog=function(){
	$("#dlg").dialog('close');
};


function close(){
	parent.util.closeDialog();
}


/*请求参数转json对象*/
function requestParamToJson(paramArray){
	var jsonObj={};
	$(paramArray).each(function(){
		jsonObj[this.name]=this.value;
	});
	util.echo(jsonObj);
	return jsonObj;
}
function changeInfo(){
	$("#editInfo").dialog('open').dialog('setTitle','修改个人信息');
}